package com.gm.commonlib.util.timeUtils;

import android.text.TextUtils;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.regex.Pattern;


public class DataFormatUtil {
    public static final DecimalFormat DF_31_10 = new DecimalFormat("##0.#");

    public static final DecimalFormat DF_31_42 = new DecimalFormat("##0.00##");

    public static final DecimalFormat DF_C1_60 = new DecimalFormat("###,###,###,##0.######");
    /**
     * TODO 整数部分至少1位，最大12(C)位
     * TODO 小数部分至少2位，最大6位
     */
    public static final DecimalFormat DF_C1_62 = new DecimalFormat("###,###,###,##0.00####");

    public static final DecimalFormat DF_C1_82 = new DecimalFormat("###,###,###,##0.00######");

    public static final DecimalFormat DF_31_30 = new DecimalFormat("##0.###");
    public static final DecimalFormat DF_30_30 = new DecimalFormat("###.###");

    /**
     * TODO 整数部分至少1位，最大12(C)位
     * TODO 小数部分至少2位，最大6位
     */
    public static final DecimalFormat DF_41_22 = new DecimalFormat("#,##0.00");

    public static final DecimalFormat DF_41_20 = new DecimalFormat("#,##0.##");

    public static final DecimalFormat DF_31_20 = new DecimalFormat("##0.##");

    public static final DecimalFormat DF_31_22 = new DecimalFormat("##0.00");

    public static final DecimalFormat DF_31_33 = new DecimalFormat("##0.000");

    public static final DecimalFormat DF_31_44 = new DecimalFormat("##0.0000");

    public static final DecimalFormat INT = new DecimalFormat("##0");

    public static final DecimalFormat DF_31_22_Prefix = new DecimalFormat("#,##0.00");
    public static final DecimalFormat RMB_Simple_Prefix = new DecimalFormat("￥##0.00元");
    public static final DecimalFormat RMB_Simple = new DecimalFormat("##0.00元");
    public static final DecimalFormat RMB_Flex = new DecimalFormat("#,##0.00元");
    public static final DecimalFormat RMB_DTail = new DecimalFormat("##0.##元");

    /**
     * TODO 整数部分至少1位，最大12(C)位
     * TODO 小数部分至少2位，最大6位
     */
    public static final DecimalFormat DF_41_00 = new DecimalFormat("#,##0");

    /**
     * 数字每隔3位加逗号
     *
     * @param data
     * @return
     */
    public static String formatTosepara(double data) {
        if (data == 0) {
            return DF_41_20.format(data);
        } else {
            return DF_41_22.format(data);
        }
    }

    public static String formatTosepara3(double data) {
        return DF_41_22.format(data);
    }

    public static String formatToseparaDouble(float data) {
        return DF_41_00.format(data);
    }

    public static String formatToseparaDouble(double data) {
        return DF_41_20.format(data);
    }

    /**
     * 保留3位小数
     *
     * @param data
     * @return
     */
    public static String changeDataThreeFloat(float data) {
        return DF_31_30.format(data);
    }

    public static boolean isInteger(String str) {
        Pattern pattern = Pattern.compile("^[-\\+]?[\\d]*$");
        return pattern.matcher(str).matches();
    }

    public static void main(String[] args) {
        double formatData = 12312312312.82342323;
        System.out.println(DF_41_22.format(formatData));
        System.out.println(DF_C1_62.format(formatData));
        System.out.println(DF_41_00.format(formatData));
    }

    public static DecimalFormat buildFormat(String format) {
        return buildFormat(format, null);
    }

    public static DecimalFormat buildFormat(String format, String unit) {
        if(format == null) {
            throw new RuntimeException("format is null!");
        }
        if(!TextUtils.isEmpty(unit)) {
            return new DecimalFormat(format + unit);
        } else {
            return new DecimalFormat(format);
        }
    }
    public static String format(Number value, String format) {
        return format(value, format, null);
    }


    public static String format(Number value, String format, String unit) {
        return format(value, buildFormat(format, unit));
    }

    public static String format(Number value, DecimalFormat format) {
        if (format != null && value != null) {
            return format.format(value);
        }
        return "";
    }

    public static String FORMAT_V1 = "yyyy-MM-dd";
    public static String FORMAT_V2 = "yyyy.MM.dd";

    private static HashMap<String, SimpleDateFormat> formatHashMap = new HashMap<String, SimpleDateFormat>();

    static {
        formatHashMap.put(FORMAT_V1, DateTimeUtils.createSimpleDateFormat(FORMAT_V1));
        formatHashMap.put(FORMAT_V2, DateTimeUtils.createSimpleDateFormat(FORMAT_V2));
    }

    public static String formatLongTime(long date, String format) {
        String result = "No:"+date;
        SimpleDateFormat formator = formatHashMap.get(format);
        if(format == null) {
            try {
                formator = DateTimeUtils.createSimpleDateFormat(format);
                result = formator.format(date);
                formatHashMap.put(format, formator);
            } catch (Exception e) {
                result = "No:"+date;
            }
        } else {
            result = formator.format(date);
        }
        return result;
    }

}
